package com.zx.cs.service.impl;

import java.util.List;

import com.zx.cs.domain.Inventory;
import com.zx.cs.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zx.cs.mapper.InventoryReplenishMapper;
import com.zx.cs.domain.InventoryReplenish;
import com.zx.cs.service.IInventoryReplenishService;

/**
 * 库存补货记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-05
 */
@Service
public class InventoryReplenishServiceImpl implements IInventoryReplenishService {

    @Autowired
    private InventoryReplenishMapper inventoryReplenishMapper;

    @Autowired
    private IInventoryService inventoryService;

    /**
     * 查询库存补货记录列表
     *
     * @param inventoryReplenish 库存补货记录
     * @return 库存补货记录
     */
    @Override
    public List<InventoryReplenish> selectInventoryReplenishList(InventoryReplenish inventoryReplenish) {
        return inventoryReplenishMapper.selectInventoryReplenishList(inventoryReplenish);
    }

    /**
     * 根据id查询库存补货记录
     *
     * @param id 库存补货记录主键
     * @return 库存补货记录
     */
    @Override
    public InventoryReplenish selectInventoryReplenishById(Long id) {
        return inventoryReplenishMapper.selectInventoryReplenishById(id);
    }

    /**
     * 新增库存补货记录
     *
     * @param inventoryReplenish 库存补货记录
     * @return 结果
     */
    @Override
    public int insertInventoryReplenish(InventoryReplenish inventoryReplenish) {
        // 验证 inventoryId 是否有效
        Long inventoryId = inventoryReplenish.getInventoryId();
        if (inventoryId != null) {
            Inventory inventory = inventoryService.selectInventoryById(inventoryId);
            if (inventory == null) {
                throw new RuntimeException("无效的库存ID: " + inventoryId);
            }
        }
        return inventoryReplenishMapper.insertInventoryReplenish(inventoryReplenish);
    }

    /**
     * 修改库存补货记录
     *
     * @param inventoryReplenish 库存补货记录
     * @return 结果
     */
    @Override
    public int updateInventoryReplenish(InventoryReplenish inventoryReplenish) {
        // 验证 inventoryId 是否有效
        Long inventoryId = inventoryReplenish.getInventoryId();
        if (inventoryId != null) {
            Inventory inventory = inventoryService.selectInventoryById(inventoryId);
            if (inventory == null) {
                throw new RuntimeException("无效的库存ID: " + inventoryId);
            }
        }
        return inventoryReplenishMapper.updateInventoryReplenish(inventoryReplenish);
    }

    /**
     * 批量删除库存补货记录
     *
     * @param ids 需要删除的库存补货记录主键
     * @return 结果
     */
    @Override
    public int deleteInventoryReplenishByIds(Long[] ids) {
        return inventoryReplenishMapper.deleteInventoryReplenishByIds(ids);
    }

    /**
     * 删除库存补货记录信息
     *
     * @param id 库存补货记录主键
     * @return 结果
     */
    @Override
    public int deleteInventoryReplenishByIds(Long id) {
        return inventoryReplenishMapper.deleteInventoryReplenishById(id);
    }
}
